package com.cenntro.nearr.service.impl;


import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cenntro.nearr.base.BaseMapper;
import com.cenntro.nearr.base.BaseServiceImpl;
import com.cenntro.nearr.mapper.AddressMapper;
import com.cenntro.nearr.mapper.LocationMapper;
import com.cenntro.nearr.service.AddressService;
import com.cenntro.nearr.vo.AddressVO;
import com.cenntro.nearr.vo.LocationVO;

@Service
public class AddressServiceImpl extends BaseServiceImpl<AddressVO> implements AddressService{

	@Resource
	private AddressMapper mapper;
	
	@Resource
	private LocationMapper locationMapper;
	
	
	@Override
	public BaseMapper<AddressVO> getMapper() {
		return mapper;
	}

	
	/**
	 * 地址保存
	 */
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public AddressVO saveOrUpdate(AddressVO addr) {
		LocationVO locationVO = new LocationVO();
		locationVO.setBlockId(addr.getBlockId());
		locationVO.setBuildingType(addr.getBuildingType());
		locationVO.setCdt(new Date());
		locationVO.setCity(addr.getCity());
		locationVO.setCountry(addr.getCountry());
		locationVO.setCreateUser(addr.getCreateUser());
		locationVO.setLat(addr.getLat());
		locationVO.setLng(addr.getLng());
		locationVO.setPicture(addr.getPicture());
		locationVO.setRemark(addr.getRemark());
		locationVO.setState(addr.getState());
		locationVO.setZipCode(addr.getZipCode());
		locationVO.setAddress(addr.getAddress());
		locationVO.setLocationId(addr.getLocationId());
		locationVO.setCreateUser(addr.getCreateUser());
		locationVO.setCdt(new Date());
		if (addr.getLocationId() != null){
			locationMapper.update(locationVO);
		}else {
			locationMapper.insert(locationVO);
		}
		
		addr.setLocationId(locationVO.getLocationId());
		addr.setCreateUser(addr.getCreateUser());
		addr.setCdt(new Date());
		if(addr.getAddressId() != null){
			this.update(addr);
		}else {
			mapper.insert(addr);
		}
		return addr;
	}
	
	public int deteleById(Integer addressId){
		int i = mapper.deleteById(addressId);
		return i;
	}
	
	
}
